package com.csun.dsms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.JyProblemDetailBackupMapper;
import com.csun.dsms.mapper.JyProblemDetailMapper;
import com.csun.dsms.mapper.JyProblemReportBackupMapper;
import com.csun.dsms.mapper.JyProblemReportMapper;
import com.csun.dsms.model.education.JyProblemDetail;
import com.csun.dsms.model.education.JyProblemReport;
import com.csun.dsms.model.education.JyProblemReportBackup;
import com.csun.dsms.model.education.JyQueryDto;
import com.csun.dsms.service.JyProblemReportService;
import com.csun.dsms.utils.ExcelUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class JyProblemReportServiceImpl  extends BaseService<JyProblemReport>
        implements JyProblemReportService {

    @Resource
    private JyProblemReportMapper jyProblemReportMapper;

    @Resource
    private JyProblemDetailMapper jyProblemDetailMapper;

    @Resource
    private JyProblemReportBackupMapper jyProblemReportBackupMapper;

    @Resource
    private JyProblemDetailBackupMapper jyProblemDetailBackupMapper;

    @Override
    public PageInfo queryListWithPage(JyQueryDto jyQueryDto) {
        PageHelper.startPage(jyQueryDto.getPageNum(), jyQueryDto.getPageSize());
        List<JyProblemReport> jyProblemReports = jyProblemReportMapper.queryListWithPage(jyQueryDto);
        String areaName = jyQueryDto.getAreaName();
        if (StrUtil.isNotBlank(areaName)){
            for (JyProblemReport jyProblemReport : jyProblemReports) {
                String prominentIssue = jyProblemReport.getProminentIssue();
                Long id = jyProblemReport.getId();
                String task1Name = jyProblemReport.getTask1Name();
                String task2Name = jyProblemReport.getTask2Name();
                String task3Name = jyProblemReport.getTask3Name();
                String task4Name = jyProblemReport.getTask4Name();
                String task5Name = jyProblemReport.getTask5Name();
                String task6Name = jyProblemReport.getTask6Name();
                String task7Name = jyProblemReport.getTask7Name();
                JyProblemDetail queryJyProblemDetail = new JyProblemDetail();
                queryJyProblemDetail.setRegion(areaName);
                queryJyProblemDetail.setProject(prominentIssue);
                List<JyProblemDetail> jyProblemDetails = jyProblemDetailMapper.select(queryJyProblemDetail);
                boolean isShowDetail = true;
                if (jyProblemReport.getId() == 19L || jyProblemReport.getId() == 22L ||
                        jyProblemReport.getId() == 23L || jyProblemReport.getId() == 31L){
                    if (areaName.equals("市本级")){
                        isShowDetail = false;
                    }
                }
                if (isShowDetail && CollectionUtil.isEmpty(jyProblemDetails)){
                    if (StrUtil.isNotEmpty(task1Name)){
                        JyProblemDetail jyProblemDetail = new JyProblemDetail();
                        jyProblemDetail.setProject(prominentIssue);
                        jyProblemDetail.setContent(task1Name);
                        jyProblemDetail.setField1("-");
                        jyProblemDetail.setField2("-");
                        jyProblemDetail.setField3("-");
                        jyProblemDetail.setField4("-");
                        jyProblemDetail.setField5("-");
                        jyProblemDetail.setField6("-");
                        jyProblemDetail.setField7("-");
                        jyProblemDetails.add(jyProblemDetail);
                    }
                    if (StrUtil.isNotEmpty(task2Name)){
                        JyProblemDetail jyProblemDetail = new JyProblemDetail();
                        jyProblemDetail.setProject(prominentIssue);
                        jyProblemDetail.setContent(task2Name);
                        jyProblemDetail.setField1("-");
                        jyProblemDetail.setField2("-");
                        jyProblemDetail.setField3("-");
                        jyProblemDetail.setField4("-");
                        jyProblemDetail.setField5("-");
                        jyProblemDetail.setField6("-");
                        jyProblemDetail.setField7("-");
                        jyProblemDetails.add(jyProblemDetail);
                    }
                    if (StrUtil.isNotEmpty(task3Name)){
                        JyProblemDetail jyProblemDetail = new JyProblemDetail();
                        jyProblemDetail.setProject(prominentIssue);
                        jyProblemDetail.setContent(task3Name);
                        jyProblemDetail.setField1("-");
                        jyProblemDetail.setField2("-");
                        jyProblemDetail.setField3("-");
                        jyProblemDetail.setField4("-");
                        jyProblemDetail.setField5("-");
                        jyProblemDetail.setField6("-");
                        jyProblemDetail.setField7("-");
                        jyProblemDetails.add(jyProblemDetail);
                    }
                    if (StrUtil.isNotEmpty(task4Name)){
                        JyProblemDetail jyProblemDetail = new JyProblemDetail();
                        jyProblemDetail.setProject(prominentIssue);
                        jyProblemDetail.setContent(task4Name);
                        jyProblemDetail.setField1("-");
                        jyProblemDetail.setField2("-");
                        jyProblemDetail.setField3("-");
                        jyProblemDetail.setField4("-");
                        jyProblemDetail.setField5("-");
                        jyProblemDetail.setField6("-");
                        jyProblemDetail.setField7("-");
                        jyProblemDetails.add(jyProblemDetail);
                    }
                    if (StrUtil.isNotEmpty(task5Name)){
                        JyProblemDetail jyProblemDetail = new JyProblemDetail();
                        jyProblemDetail.setProject(prominentIssue);
                        jyProblemDetail.setContent(task5Name);
                        jyProblemDetail.setField1("-");
                        jyProblemDetail.setField2("-");
                        jyProblemDetail.setField3("-");
                        jyProblemDetail.setField4("-");
                        jyProblemDetail.setField5("-");
                        jyProblemDetail.setField6("-");
                        jyProblemDetail.setField7("-");
                        jyProblemDetails.add(jyProblemDetail);
                    }
                    if (StrUtil.isNotEmpty(task6Name)){
                        JyProblemDetail jyProblemDetail = new JyProblemDetail();
                        jyProblemDetail.setProject(prominentIssue);
                        jyProblemDetail.setContent(task6Name);
                        jyProblemDetail.setField1("-");
                        jyProblemDetail.setField2("-");
                        jyProblemDetail.setField3("-");
                        jyProblemDetail.setField4("-");
                        jyProblemDetail.setField5("-");
                        jyProblemDetail.setField6("-");
                        jyProblemDetail.setField7("-");
                        jyProblemDetails.add(jyProblemDetail);
                    }
                    if (StrUtil.isNotEmpty(task7Name)){
                        JyProblemDetail jyProblemDetail = new JyProblemDetail();
                        jyProblemDetail.setProject(prominentIssue);
                        jyProblemDetail.setContent(task7Name);
                        jyProblemDetail.setField1("-");
                        jyProblemDetail.setField2("-");
                        jyProblemDetail.setField3("-");
                        jyProblemDetail.setField4("-");
                        jyProblemDetail.setField5("-");
                        jyProblemDetail.setField6("-");
                        jyProblemDetail.setField7("-");
                        jyProblemDetails.add(jyProblemDetail);
                    }
                }
                if (CollectionUtil.isNotEmpty(jyProblemDetails)){
                    for (JyProblemDetail jyProblemDetail : jyProblemDetails) {
                        String content = jyProblemDetail.getContent();
                        String field1 = jyProblemDetail.getField1();
                        String field2 = jyProblemDetail.getField2();
                        String field3 = jyProblemDetail.getField3();
                        String field4 = jyProblemDetail.getField4();
                        String field5 = jyProblemDetail.getField5();
                        String field6 = jyProblemDetail.getField6();
                        String field7 = jyProblemDetail.getField7();
                        if(StrUtil.isEmpty(field1)){
                            field1 = "-";
                        }
                        if(StrUtil.isEmpty(field2)){
                            field2 = "-";
                        }
                        if(StrUtil.isEmpty(field3)){
                            field3 = "-";
                        }
                        if(StrUtil.isEmpty(field4)){
                            field4 = "-";
                        }
                        if(StrUtil.isEmpty(field5)){
                            field5 = "-";
                        }
                        if(StrUtil.isEmpty(field6)){
                            field6 = "-";
                        }
                        if(StrUtil.isEmpty(field7)){
                            field7 = "-";
                        }
                        String task1Json = jyProblemReport.getTask1Json();
                        String task2Json = jyProblemReport.getTask2Json();
                        String task3Json = jyProblemReport.getTask3Json();
                        String task4Json = jyProblemReport.getTask4Json();
                        String task5Json = jyProblemReport.getTask5Json();
                        String task6Json = jyProblemReport.getTask6Json();
                        String task7Json = jyProblemReport.getTask7Json();
                        if (isJsonArray(task1Json)) {
                            if (content.equals(task1Name)||id==18) {
                                JSONArray task1JsonArray = JSONArray.parseArray(task1Json);
                                JSONArray task1JsonArrayNew = new JSONArray();
                                for (int i = 0; i < task1JsonArray.size(); i++) {
                                    JSONObject task1JsonObject = task1JsonArray.getJSONObject(i);
                                    if (i == 0) {
                                        task1JsonObject.put("value", field1);
                                    } else if (i == 1) {
                                        task1JsonObject.put("value", field2);
                                    } else if (i == 2) {
                                        task1JsonObject.put("value", field3);
                                    } else if (i == 3) {
                                        task1JsonObject.put("value", field4);
                                    } else if (i == 4) {
                                        task1JsonObject.put("value", field5);
                                    } else if (i == 5) {
                                        task1JsonObject.put("value", field6);
                                    } else if (i == 6) {
                                        task1JsonObject.put("value", field7);
                                    }
                                    task1JsonArrayNew.add(task1JsonObject);
                                    jyProblemReport.setTask1Json(task1JsonArrayNew.toJSONString());
                                }
                            }
                            if (content.equals(task2Name)||id==18) {
                                JSONArray task2JsonArray = JSONArray.parseArray(task2Json);
                                JSONArray task2JsonArrayNew = new JSONArray();
                                for (int i = 0; i < task2JsonArray.size(); i++) {
                                    JSONObject task2JsonObject = task2JsonArray.getJSONObject(i);
                                    if (i == 0) {
                                        task2JsonObject.put("value", field1);
                                    } else if (i == 1) {
                                        task2JsonObject.put("value", field2);
                                    } else if (i == 2) {
                                        task2JsonObject.put("value", field3);
                                    } else if (i == 3) {
                                        task2JsonObject.put("value", field4);
                                    } else if (i == 4) {
                                        task2JsonObject.put("value", field5);
                                    } else if (i == 5) {
                                        task2JsonObject.put("value", field6);
                                    } else if (i == 6) {
                                        task2JsonObject.put("value", field7);
                                    }
                                    System.out.println(task2JsonObject);
                                    task2JsonArrayNew.add(task2JsonObject);
                                    jyProblemReport.setTask2Json(task2JsonArrayNew.toJSONString());
                                }
                            }
                            if (content.equals(task3Name)||id==18) {
                                JSONArray task3JsonArray = JSONArray.parseArray(task3Json);
                                JSONArray task3JsonArrayNew = new JSONArray();
                                for (int i = 0; i < task3JsonArray.size(); i++) {
                                    JSONObject task3JsonObject = task3JsonArray.getJSONObject(i);
                                    if (i == 0) {
                                        task3JsonObject.put("value", field1);
                                    } else if (i == 1) {
                                        task3JsonObject.put("value", field2);
                                    } else if (i == 2) {
                                        task3JsonObject.put("value", field3);
                                    } else if (i == 3) {
                                        task3JsonObject.put("value", field4);
                                    } else if (i == 4) {
                                        task3JsonObject.put("value", field5);
                                    } else if (i == 5) {
                                        task3JsonObject.put("value", field6);
                                    } else if (i == 6) {
                                        task3JsonObject.put("value", field7);
                                    }
                                    task3JsonArrayNew.add(task3JsonObject);
                                    jyProblemReport.setTask3Json(task3JsonArrayNew.toJSONString());
                                }
                            }
                            if (content.equals(task4Name)||id==18) {
                                JSONArray task4JsonArray = JSONArray.parseArray(task4Json);
                                JSONArray task4JsonArrayNew = new JSONArray();
                                for (int i = 0; i < task4JsonArray.size(); i++) {
                                    JSONObject task4JsonObject = task4JsonArray.getJSONObject(i);
                                    if (i == 0) {
                                        task4JsonObject.put("value", field1);
                                    } else if (i == 1) {
                                        task4JsonObject.put("value", field2);
                                    } else if (i == 2) {
                                        task4JsonObject.put("value", field3);
                                    } else if (i == 3) {
                                        task4JsonObject.put("value", field4);
                                    } else if (i == 4) {
                                        task4JsonObject.put("value", field5);
                                    } else if (i == 5) {
                                        task4JsonObject.put("value", field6);
                                    } else if (i == 6) {
                                        task4JsonObject.put("value", field7);
                                    }
                                    task4JsonArrayNew.add(task4JsonObject);
                                    jyProblemReport.setTask4Json(task4JsonArrayNew.toJSONString());
                                }
                            }
                            if (content.equals(task5Name)||id==18) {
                                JSONArray task5JsonArray = JSONArray.parseArray(task5Json);
                                JSONArray task5JsonArrayNew = new JSONArray();
                                for (int i = 0; i < task5JsonArray.size(); i++) {
                                    JSONObject task5JsonObject = task5JsonArray.getJSONObject(i);
                                    if (i == 0) {
                                        task5JsonObject.put("value", field1);
                                    } else if (i == 1) {
                                        task5JsonObject.put("value", field2);
                                    } else if (i == 2) {
                                        task5JsonObject.put("value", field3);
                                    } else if (i == 3) {
                                        task5JsonObject.put("value", field4);
                                    } else if (i == 4) {
                                        task5JsonObject.put("value", field5);
                                    } else if (i == 5) {
                                        task5JsonObject.put("value", field6);
                                    } else if (i == 6) {
                                        task5JsonObject.put("value", field7);
                                    }
                                    task5JsonArrayNew.add(task5JsonObject);
                                    jyProblemReport.setTask5Json(task5JsonArrayNew.toJSONString());
                                }
                            }
                            if (content.equals(task6Name)||id==18) {
                                JSONArray task6JsonArray = JSONArray.parseArray(task6Json);
                                JSONArray task6JsonArrayNew = new JSONArray();
                                for (int i = 0; i < task6JsonArray.size(); i++) {
                                    JSONObject task6JsonObject = task6JsonArray.getJSONObject(i);
                                    if (i == 0) {
                                        task6JsonObject.put("value", field1);
                                    } else if (i == 1) {
                                        task6JsonObject.put("value", field2);
                                    } else if (i == 2) {
                                        task6JsonObject.put("value", field3);
                                    } else if (i == 3) {
                                        task6JsonObject.put("value", field4);
                                    } else if (i == 4) {
                                        task6JsonObject.put("value", field5);
                                    } else if (i == 5) {
                                        task6JsonObject.put("value", field6);
                                    } else if (i == 6) {
                                        task6JsonObject.put("value", field7);
                                    }
                                    task6JsonArrayNew.add(task6JsonObject);
                                    jyProblemReport.setTask6Json(task6JsonArrayNew.toJSONString());
                                }
                            }
                            if (content.equals(task7Name)||id==18) {
                                JSONArray task7JsonArray = JSONArray.parseArray(task7Json);
                                JSONArray task7JsonArrayNew = new JSONArray();
                                for (int i = 0; i < task7JsonArray.size(); i++) {
                                    JSONObject task7JsonObject = task7JsonArray.getJSONObject(i);
                                    if (i == 0) {
                                        task7JsonObject.put("value", field1);
                                    } else if (i == 1) {
                                        task7JsonObject.put("value", field2);
                                    } else if (i == 2) {
                                        task7JsonObject.put("value", field3);
                                    } else if (i == 3) {
                                        task7JsonObject.put("value", field4);
                                    } else if (i == 4) {
                                        task7JsonObject.put("value", field5);
                                    } else if (i == 5) {
                                        task7JsonObject.put("value", field6);
                                    } else if (i == 6) {
                                        task7JsonObject.put("value", field7);
                                    }
                                    task7JsonArrayNew.add(task7JsonObject);
                                    jyProblemReport.setTask7Json(task7JsonArrayNew.toJSONString());
                                }
                            }
                        }else{
                            jyProblemReport.setTask1Json(field1);
                            jyProblemReport.setTask2Json(field2);
                            jyProblemReport.setTask3Json(field3);
                            jyProblemReport.setTask4Json(field4);
                            jyProblemReport.setTask5Json(field5);
                            jyProblemReport.setTask6Json(field6);
                            jyProblemReport.setTask7Json(field7);
                        }
                    }
                }

            }
        }
        return new PageInfo<>(jyProblemReports);
    }

    @Override
    public Wrapper<Object> importData(MultipartFile file) {
        return null;
    }

    @Override
    public void downExcelData(HttpServletResponse response) {

    }

    @Override
    public void exportData(HttpServletResponse response, JyQueryDto jyQueryDto) {
        try{
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("jzsj", DateUtil.date());

            PageHelper.startPage(jyQueryDto.getPageNum(), jyQueryDto.getPageSize());
            List<JyProblemReport> jyProblemReports = jyProblemReportMapper.queryListWithPage(jyQueryDto);
            // 获取最新date
            Long bfTime = 0l;
            if (CollectionUtil.isEmpty(jyProblemReports)){
                return;
            }
            for (JyProblemReport jyProblemReport : jyProblemReports) {
                Long id = jyProblemReport.getId();
                if (jyProblemReport.getCreatedTime().getTime()>bfTime){
                    bfTime = jyProblemReport.getCreatedTime().getTime();
                }
                String prominentIssue = jyProblemReport.getProminentIssue();
                String issueType = jyProblemReport.getIssueType();
                String issueCategory = jyProblemReport.getIssueCategory();
                String leadingLeader = jyProblemReport.getLeadingLeader();
                String leadingUnit = jyProblemReport.getLeadingUnit();
                String task1Name = jyProblemReport.getTask1Name();
                String task1Json = jyProblemReport.getTask1Json();
                String task2Name = jyProblemReport.getTask2Name();
                String task2Json = jyProblemReport.getTask2Json();
                String task3Name = jyProblemReport.getTask3Name();
                String task3Json = jyProblemReport.getTask3Json();
                String task4Name = jyProblemReport.getTask4Name();
                String task4Json = jyProblemReport.getTask4Json();
                String task5Name = jyProblemReport.getTask5Name();
                String task5Json = jyProblemReport.getTask5Json();
                String task6Name = jyProblemReport.getTask6Name();
                String task6Json = jyProblemReport.getTask6Json();
                String task7Name = jyProblemReport.getTask7Name();
                String task7Json = jyProblemReport.getTask7Json();
                Integer accountabilityCount = jyProblemReport.getAccountabilityCount();
                Integer regulationsPlanCount = jyProblemReport.getRegulationsPlanCount();
                Integer regulationsCompletedCount = jyProblemReport.getRegulationsCompletedCount();
                String regulationsPlanJson = jyProblemReport.getRegulationsPlanJson();
                String topThreeEvaluation = jyProblemReport.getTopThreeEvaluation();
                String bottomThreeEvaluation = jyProblemReport.getBottomThreeEvaluation();
                String remarks = jyProblemReport.getRemarks();
                params.put("prominentIssue"+id, prominentIssue==null?"":prominentIssue);
                params.put("issueType"+id, issueType==null?"":issueType);
                params.put("issueCategory"+id, issueCategory==null?"":issueCategory);
                params.put("leadingLeader"+id, leadingLeader==null?"":leadingLeader);
                params.put("leadingUnit"+id, leadingUnit==null?"":leadingUnit);
                params.put("task1Name"+id, task1Name==null?"":task1Name);
                params.put("task2Name"+id, task2Name==null?"":task2Name);
                params.put("task3Name"+id, task3Name==null?"":task3Name);
                params.put("task4Name"+id, task4Name==null?"":task4Name);
                params.put("task5Name"+id, task5Name==null?"":task5Name);
                params.put("task6Name"+id, task6Name==null?"":task6Name);
                params.put("task7Name"+id, task7Name==null?"":task7Name);
                params.put("accountabilityCount"+id, accountabilityCount==null?0:accountabilityCount);
                params.put("regulationsPlanCount"+id, regulationsPlanCount==null?0:regulationsPlanCount);
                params.put("regulationsPlanCount"+id, regulationsPlanCount==null?0:regulationsPlanCount);
                params.put("regulationsCompletedCount"+id, regulationsCompletedCount==null?0:regulationsCompletedCount);
                params.put("topThreeEvaluation"+id, topThreeEvaluation==null?"":topThreeEvaluation);
                params.put("bottomThreeEvaluation"+id, bottomThreeEvaluation==null?"":bottomThreeEvaluation);
                params.put("remarks"+id, remarks==null?"":remarks);
                if(StrUtil.isNotBlank(regulationsPlanJson)){
                    JSONArray regulationsPlanJsonList = JSONArray.parseArray(regulationsPlanJson);
                    StringBuilder regulationsPlanSB = new StringBuilder();
                    for (int i = 0; i < regulationsPlanJsonList.size(); i++) {
                        JSONObject regulationsPlanJsonObj = regulationsPlanJsonList.getJSONObject(i);
                        String name = regulationsPlanJsonObj.getString("name");
                        String status = regulationsPlanJsonObj.getString("status");
                        if ("1".equals(status)){
                            regulationsPlanSB.append("√[已建立] ");
                        }else if("0".equals(status)){
                            regulationsPlanSB.append("×[未建立] ");
                        }
                        regulationsPlanSB.append(name).append("\n");
                    }
                    String regulationsPlanString = regulationsPlanSB.toString();
                    params.put("regulationsPlanJson"+id, StrUtil.isNotBlank(regulationsPlanString)?regulationsPlanString:"");
                }else{
                    params.put("regulationsPlanJson"+id, "");
                }

                if(StrUtil.isNotBlank(task1Json)) {
                    if (isJsonArray(task1Json)) {
                        JSONArray task1JsonArray = JSONArray.parseArray(task1Json);
                        for (int i = 0; i < task1JsonArray.size(); i++) {
                            JSONObject task1JsonObject = (JSONObject) task1JsonArray.get(i);
                            params.put("task1Json" + id+"Name"+(i+1), task1JsonObject.getString("name"));
                            params.put("task1Json" + id+"Value"+(i+1), task1JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task1Json" + id, task1Json);
                    }
                }else{
                    params.put("task1Json"+id, "");
                }
                if(StrUtil.isNotBlank(task2Json)) {
                    if (isJsonArray(task2Json)) {
                        JSONArray task2JsonArray = JSONArray.parseArray(task2Json);
                        for (int i = 0; i < task2JsonArray.size(); i++) {
                            JSONObject task2JsonObject = (JSONObject) task2JsonArray.get(i);
                            params.put("task2Json" + id+"Name"+(i+1), task2JsonObject.getString("name"));
                            params.put("task2Json" + id+"Value"+(i+1), task2JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task2Json" + id, task2Json);
                    }
                }else{
                    params.put("task2Json"+id, "");
                }
                if(StrUtil.isNotBlank(task3Json)) {
                    if (isJsonArray(task3Json)) {
                        JSONArray task3JsonArray = JSONArray.parseArray(task3Json);
                        for (int i = 0; i < task3JsonArray.size(); i++) {
                            JSONObject task3JsonObject = (JSONObject) task3JsonArray.get(i);
                            params.put("task3Json" + id+"Name"+(i+1), task3JsonObject.getString("name"));
                            params.put("task3Json" + id+"Value"+(i+1), task3JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task3Json" + id, task3Json);
                    }
                }else{
                    params.put("task3Json"+id, "");
                }
                if(StrUtil.isNotBlank(task4Json)) {
                    if (isJsonArray(task4Json)) {
                        JSONArray task4JsonArray = JSONArray.parseArray(task4Json);
                        for (int i = 0; i < task4JsonArray.size(); i++) {
                            JSONObject task4JsonObject = (JSONObject) task4JsonArray.get(i);
                            params.put("task4Json" + id+"Name"+(i+1), task4JsonObject.getString("name"));
                            params.put("task4Json" + id+"Value"+(i+1), task4JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task4Json" + id, task4Json);
                    }
                }else{
                    params.put("task4Json"+id, "");
                }
                if(StrUtil.isNotBlank(task5Json)) {
                    if (isJsonArray(task5Json)) {
                        JSONArray task5JsonArray = JSONArray.parseArray(task5Json);
                        for (int i = 0; i < task5JsonArray.size(); i++) {
                            JSONObject task5JsonObject = (JSONObject) task5JsonArray.get(i);
                            params.put("task5Json" + id+"Name"+(i+1), task5JsonObject.getString("name"));
                            params.put("task5Json" + id+"Value"+(i+1), task5JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task5Json" + id, task5Json);
                    }
                }else{
                    params.put("task5Json"+id, "");
                }
                if(StrUtil.isNotBlank(task6Json)) {
                    if (isJsonArray(task6Json)) {
                        JSONArray task6JsonArray = JSONArray.parseArray(task6Json);
                        for (int i = 0; i < task6JsonArray.size(); i++) {
                            JSONObject task6JsonObject = (JSONObject) task6JsonArray.get(i);
                            params.put("task6Json" + id+"Name"+(i+1), task6JsonObject.getString("name"));
                            params.put("task6Json" + id+"Value"+(i+1), task6JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task6Json" + id, task6Json);
                    }
                }else{
                    params.put("task6Json"+id, "");
                }

                if(StrUtil.isNotBlank(task7Json)) {
                    if (isJsonArray(task7Json)) {
                        JSONArray task7JsonArray = JSONArray.parseArray(task7Json);
                        for (int i = 0; i < task7JsonArray.size(); i++) {
                            JSONObject task7JsonObject = (JSONObject) task7JsonArray.get(i);
                            params.put("task7Json" + id+"Name"+(i+1), task7JsonObject.getString("name"));
                            params.put("task7Json" + id+"Value"+(i+1), task7JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task7Json" + id, task7Json);
                    }
                }else{
                    params.put("task7Json"+id, "");
                }
            }

            params.put("jzsj", DateUtil.date(bfTime));

            InputStream is = getClass().getClassLoader().getResourceAsStream("template/xxjyjzmb.xlsx");
            ExcelUtils excelUtils = new ExcelUtils();
            try {
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                response.setCharacterEncoding("utf-8");
                response.setHeader("Content-Disposition", "attachment;filename=exportData.xlsx");
                try (InputStream inputStream = excelUtils.replaceExcel(is, params);
                     ServletOutputStream out = response.getOutputStream()) {
                    IoUtil.copy(inputStream, out);
                    out.flush();
                } catch (IOException e) {
                    log.error("数据导出时发生 IO 异常", e);
                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                }
            } catch (Exception e) {
                log.error("数据导出时发生异常", e);
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void dataCombinedCalculations(JyProblemDetail jyProblemDetail) {
        try {
            DecimalFormat df = new DecimalFormat("0.00%");
            Integer id = jyProblemDetail.getId();
            JyProblemDetail queryJyProblemDetailBean = new JyProblemDetail();
            queryJyProblemDetailBean.setId(id);
            JyProblemDetail queryJyProblemDetail = jyProblemDetailMapper.selectOne(queryJyProblemDetailBean);
            if (queryJyProblemDetail != null) {
                String project = queryJyProblemDetail.getProject();
                String content = queryJyProblemDetail.getContent();
                JyProblemDetail queryJyProblemDetailNew = new JyProblemDetail();
                queryJyProblemDetailNew.setProject(project);
                queryJyProblemDetailNew.setContent(content);
                List<JyProblemDetail> jyProblemDetailList = jyProblemDetailMapper.select(queryJyProblemDetailNew);
                if (CollectionUtil.isEmpty(jyProblemDetailList)){
                    return;
                }
                JyProblemReport yuerqJyProblemReport = new JyProblemReport();
                yuerqJyProblemReport.setProminentIssue(project);
                JyProblemReport jyProblemReport = jyProblemReportMapper.selectOne(yuerqJyProblemReport);
                if (jyProblemReport == null) {
                    return;
                }

                double sum1 = jyProblemDetailList.stream()
                        .map(JyProblemDetail::getField1)
                        .filter(Objects::nonNull)
                        .filter(str -> !str.isEmpty())
                        .mapToDouble(Double::parseDouble)
                        .sum();
                double sum2 = jyProblemDetailList.stream()
                        .map(JyProblemDetail::getField2)
                        .filter(Objects::nonNull)
                        .filter(str -> !str.isEmpty())
                        .mapToDouble(Double::parseDouble)
                        .sum();
                double sum3 = jyProblemDetailList.stream()
                        .map(JyProblemDetail::getField3)
                        .filter(Objects::nonNull)
                        .filter(str -> !str.isEmpty())
                        .mapToDouble(Double::parseDouble)
                        .sum();
                double sum4 = jyProblemDetailList.stream()
                        .map(JyProblemDetail::getField4)
                        .filter(Objects::nonNull)
                        .filter(str -> !str.isEmpty())
                        .mapToDouble(Double::parseDouble)
                        .sum();
                double sum5 = jyProblemDetailList.stream()
                        .map(JyProblemDetail::getField5)
                        .filter(Objects::nonNull)
                        .filter(str -> !str.isEmpty())
                        .mapToDouble(Double::parseDouble)
                        .sum();
                double sum6 = jyProblemDetailList.stream()
                        .map(JyProblemDetail::getField6)
                        .filter(Objects::nonNull)
                        .filter(str -> !str.isEmpty())
                        .mapToDouble(Double::parseDouble)
                        .sum();
                switch (project) {
                    case "信访积案化解":
                        if("信访问题".equals(content)){
                            jyProblemReport.setTask1Json(Convert.toStr(sum1));
                            jyProblemReport.setTask2Json(Convert.toStr(sum2));
                            jyProblemReport.setTask3Json(sum1 == 0 ? "0.00%" : df.format((double) sum2 / sum1));
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;
                    case "后进村整顿":
                        if("落实领导包保责任制".equals(content)) {
                            String taskJson = jyProblemReport.getTask1Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", sum1);
                                } else if (i == 1) {
                                    task1JsonObject.put("value", sum2);
                                } else if (i == 2) {
                                    task1JsonObject.put("value", sum1 == 0 ? "0.00%" : df.format((double) sum2 / sum1));
                                }
                            }
                            jyProblemReport.setTask1Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        if("“一村一策”制定任务清单".equals(content)) {
                            String taskJson = jyProblemReport.getTask2Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", sum1);
                                } else if (i == 1) {
                                    task1JsonObject.put("value", sum2);
                                } else if (i == 2) {
                                    task1JsonObject.put("value", sum1 == 0 ? "0.00%" : df.format((double) sum2 / sum1));
                                }
                            }
                            jyProblemReport.setTask2Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        if("完成整改任务数10%".equals(content)) {
                            String taskJson = jyProblemReport.getTask3Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", sum1);
                                } else if (i == 1) {
                                    task1JsonObject.put("value", sum2);
                                } else if (i == 2) {
                                    task1JsonObject.put("value", sum1 == 0 ? "0.00%" : df.format((double) sum2 / sum1));
                                }
                            }
                            jyProblemReport.setTask3Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;
                    case "行政村布局调整后半篇文章":
                        if("行政村全面体检".equals(content)) {
                            String taskJson = jyProblemReport.getTask1Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", sum1);
                                } else if (i == 1) {
                                    task1JsonObject.put("value", sum2);
                                } else if (i == 2) {
                                    task1JsonObject.put("value", sum1 == 0 ? "0.00%" : df.format((double) sum2 / sum1));
                                }
                            }
                            jyProblemReport.setTask1Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        if("完成80%“五个一”问题村整改".equals(content)) {
                            String taskJson = jyProblemReport.getTask2Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", sum1);
                                } else if (i == 1) {
                                    task1JsonObject.put("value", sum2);
                                } else if (i == 2) {
                                    task1JsonObject.put("value", sum1 == 0 ? "0.00%" : df.format((double) sum2 / sum1));
                                }
                            }
                            jyProblemReport.setTask2Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        if("推动10%合并村“六个提升”".equals(content)) {
                            String taskJson = jyProblemReport.getTask3Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", sum1);
                                } else if (i == 1) {
                                    task1JsonObject.put("value", sum2);
                                } else if (i == 2) {
                                    task1JsonObject.put("value", sum1 == 0 ? "0.00%" : df.format((double) sum2 / sum1));
                                }
                            }
                            jyProblemReport.setTask3Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;
                    case "矛盾纠纷实质性化解":
                        if("矛盾纠纷化解率".equals(content)) {
                            String taskJson = jyProblemReport.getTask1Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            long field1Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField1())).count();
                            long field2Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField2())).count();
                            long field3Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField3())).count();

                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", field1Count == 0 ? "0.00%" : df.format((double) sum1 / field1Count));
                                } else if (i == 1) {
                                    task1JsonObject.put("value", field2Count == 0 ? "0.00%" : df.format((double) sum2 / field2Count));
                                } else if (i == 2) {
                                    task1JsonObject.put("value", field3Count == 0 ? "0.00%" : df.format((double) sum3 / field3Count));
                                }
                            }
                            jyProblemReport.setTask1Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;
                    case "道路交通安全综合治理":
                        if("全市全年亡人道路交通事故数同比下降10%".equals(content)) {
                            String taskJson = jyProblemReport.getTask1Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", sum1);
                                } else if (i == 1) {
                                    task1JsonObject.put("value", sum2);
                                } else if (i == 2) {
                                    task1JsonObject.put("value", sum1 == 0 ? "0.00%" : df.format((double) sum2 / sum1));
                                }
                            }
                            jyProblemReport.setTask1Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        if("全市全年交通事故亡人数同比下降10%".equals(content)) {
                            String taskJson = jyProblemReport.getTask2Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", sum1);
                                } else if (i == 1) {
                                    task1JsonObject.put("value", sum2);
                                } else if (i == 2) {
                                    task1JsonObject.put("value", sum1 == 0 ? "0.00%" : df.format((double) sum2 / sum1));
                                }
                            }
                            jyProblemReport.setTask2Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;
                    case "执行难破产难":
                        if("执行完毕率".equals(content)) {
                            String taskJson = jyProblemReport.getTask1Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            long field1Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField1())).count();
                            long field2Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField2())).count();
                            long field3Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField3())).count();

                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", field1Count == 0 ? "0.00%" : df.format((double) sum1 / field1Count));
                                } else if (i == 1) {
                                    task1JsonObject.put("value", field2Count == 0 ? "0.00%" : df.format((double) sum2 / field2Count));
                                } else if (i == 2) {
                                    task1JsonObject.put("value", field3Count == 0 ? "0.00%" : df.format((double) sum3 / field3Count));
                                }
                            }
                            jyProblemReport.setTask1Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        if("执行到位率".equals(content)) {
                            String taskJson = jyProblemReport.getTask2Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            long field1Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField1())).count();
                            long field2Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField2())).count();
                            long field3Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField3())).count();

                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", field1Count == 0 ? "0.00%" : df.format((double) sum1 / field1Count));
                                } else if (i == 1) {
                                    task1JsonObject.put("value", field2Count == 0 ? "0.00%" : df.format((double) sum2 / field2Count));
                                } else if (i == 2) {
                                    task1JsonObject.put("value", field3Count == 0 ? "0.00%" : df.format((double) sum3 / field3Count));
                                }
                            }
                            jyProblemReport.setTask2Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;
                    case "不担当不作为":
                        if("不担当不作为".equals(content)){
                            long field2Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField2())).count();
                            long field4Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField4())).count();
                            long field6Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField6())).count();
                            jyProblemReport.setTask1Json(Convert.toStr(sum1));
                            jyProblemReport.setTask2Json(field2Count == 0 ? "0.00%" : df.format((double) sum2 / field2Count));
                            jyProblemReport.setTask3Json(Convert.toStr(sum3));
                            jyProblemReport.setTask4Json(field4Count == 0 ? "0.00%" : df.format((double) sum4 / field4Count));
                            jyProblemReport.setTask5Json(Convert.toStr(sum5));
                            jyProblemReport.setTask6Json(field6Count == 0 ? "0.00%" : df.format((double) sum6 / field6Count));
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;
                    case "违规吃喝":
                        if("违规吃喝".equals(content)){
                            long field2Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField2())).count();
                            long field4Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField4())).count();
                            long field6Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField6())).count();
                            jyProblemReport.setTask1Json(Convert.toStr(sum1));
                            jyProblemReport.setTask2Json(field2Count == 0 ? "0.00%" : df.format((double) sum2 / field2Count));
                            jyProblemReport.setTask3Json(Convert.toStr(sum3));
                            jyProblemReport.setTask4Json(field4Count == 0 ? "0.00%" : df.format((double) sum4 / field4Count));
                            jyProblemReport.setTask5Json(Convert.toStr(sum5));
                            jyProblemReport.setTask6Json(field6Count == 0 ? "0.00%" : df.format((double) sum6 / field6Count));
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;
                    case "违规收送礼品礼金":
                        if("违规收送礼品礼金".equals(content)){
                            long field2Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField2())).count();
                            long field4Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField4())).count();
                            long field6Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField6())).count();
                            jyProblemReport.setTask1Json(Convert.toStr(sum1));
                            jyProblemReport.setTask2Json(field2Count == 0 ? "0.00%" : df.format((double) sum2 / field2Count));
                            jyProblemReport.setTask3Json(Convert.toStr(sum3));
                            jyProblemReport.setTask4Json(field4Count == 0 ? "0.00%" : df.format((double) sum4 / field4Count));
                            jyProblemReport.setTask5Json(Convert.toStr(sum5));
                            jyProblemReport.setTask6Json(field6Count == 0 ? "0.00%" : df.format((double) sum6 / field6Count));
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;
                    case "违规打牌和赌博":
                        if("违规打牌和赌博".equals(content)){
                            long field2Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField2())).count();
                            long field4Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField4())).count();
                            long field6Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField6())).count();
                            jyProblemReport.setTask1Json(Convert.toStr(sum1));
                            jyProblemReport.setTask2Json(field2Count == 0 ? "0.00%" : df.format((double) sum2 / field2Count));
                            jyProblemReport.setTask3Json(Convert.toStr(sum3));
                            jyProblemReport.setTask4Json(field4Count == 0 ? "0.00%" : df.format((double) sum4 / field4Count));
                            jyProblemReport.setTask5Json(Convert.toStr(sum5));
                            jyProblemReport.setTask6Json(field6Count == 0 ? "0.00%" : df.format((double) sum6 / field6Count));
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;
                    case "酒驾醉驾":
                        if("酒驾醉驾".equals(content)){
                            long field2Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField2())).count();
                            long field4Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField4())).count();
                            long field6Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField6())).count();
                            jyProblemReport.setTask1Json(Convert.toStr(sum1));
                            jyProblemReport.setTask2Json(field2Count == 0 ? "0.00%" : df.format((double) sum2 / field2Count));
                            jyProblemReport.setTask3Json(Convert.toStr(sum3));
                            jyProblemReport.setTask4Json(field4Count == 0 ? "0.00%" : df.format((double) sum4 / field4Count));
                            jyProblemReport.setTask5Json(Convert.toStr(sum5));
                            jyProblemReport.setTask6Json(field6Count == 0 ? "0.00%" : df.format((double) sum6 / field6Count));
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;
                    case "中小学校园餐":
                        if("“校园餐”问题".equals(content)) {
                            String taskJson = jyProblemReport.getTask1Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            long field1Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField1())).count();
                            long field2Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField2())).count();
                            long field3Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField3())).count();

                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", field1Count == 0 ? "0.00%" : df.format((double) sum1 / field1Count));
                                } else if (i == 1) {
                                    task1JsonObject.put("value", field2Count == 0 ? "0.00%" : df.format((double) sum2 / field2Count));
                                } else if (i == 2) {
                                    task1JsonObject.put("value", field3Count == 0 ? "0.00%" : df.format((double) sum3 / field3Count));
                                }
                            }
                            jyProblemReport.setTask1Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        if("执行到位率".equals(content)) {
                            String taskJson = jyProblemReport.getTask2Json();
                            JSONArray taskJsonArray = JSONArray.parseArray(taskJson);
                            long field1Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField1())).count();
                            long field2Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField2())).count();
                            long field3Count = jyProblemDetailList.stream().filter(bean -> StrUtil.isNotBlank(bean.getField3())).count();

                            for (int i = 0; i < taskJsonArray.size(); i++) {
                                JSONObject task1JsonObject = taskJsonArray.getJSONObject(i);
                                if (i == 0) {
                                    task1JsonObject.put("value", field1Count == 0 ? "0.00%" : df.format((double) sum1 / field1Count));
                                } else if (i == 1) {
                                    task1JsonObject.put("value", field2Count == 0 ? "0.00%" : df.format((double) sum2 / field2Count));
                                } else if (i == 2) {
                                    task1JsonObject.put("value", field3Count == 0 ? "0.00%" : df.format((double) sum3 / field3Count));
                                }
                            }
                            jyProblemReport.setTask2Json(taskJsonArray.toJSONString());
                            jyProblemReportMapper.updateByPrimaryKeySelective(jyProblemReport);
                        }
                        break;

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportBackupData(HttpServletResponse response,  JSONObject reqJson) {
        try{
            String backup = reqJson.getString("backup");
            DateTime backupDate = DateUtil.parse(backup);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("jzsj", backupDate);

            JyProblemReportBackup jyProblemReportBackup = new JyProblemReportBackup();
            String backupStr = DateUtil.format(backupDate, DatePattern.PURE_DATE_PATTERN);
            jyProblemReportBackup.setBackup(backupStr);
            List<JyProblemReportBackup> JyProblemReportBackups = jyProblemReportBackupMapper.select(jyProblemReportBackup);
            List<JyProblemReport> jyProblemReports = jyProblemReportMapper.selectAll();
            for (JyProblemReport jyProblemReport : jyProblemReports) {
                Long id = jyProblemReport.getId();
                String prominentIssue = jyProblemReport.getProminentIssue();
                JyProblemReportBackup jyProblemReportBackup1 = JyProblemReportBackups.stream().filter(bean -> bean.getProminentIssue().equals(prominentIssue)).collect(Collectors.toList()).get(0);
                jyProblemReportBackup1.setId(null);
                BeanUtil.copyProperties(jyProblemReportBackup1, jyProblemReport);
                jyProblemReport.setId(id);
            }
            // 获取最新date
            Long bfTime = 0l;
            if (CollectionUtil.isEmpty(jyProblemReports)){
                return;
            }
            for (JyProblemReport jyProblemReport : jyProblemReports) {
                Long id = jyProblemReport.getId();
                if (jyProblemReport.getCreatedTime().getTime()>bfTime){
                    bfTime = jyProblemReport.getCreatedTime().getTime();
                }
                String prominentIssue = jyProblemReport.getProminentIssue();
                String issueType = jyProblemReport.getIssueType();
                String issueCategory = jyProblemReport.getIssueCategory();
                String leadingLeader = jyProblemReport.getLeadingLeader();
                String leadingUnit = jyProblemReport.getLeadingUnit();
                String task1Name = jyProblemReport.getTask1Name();
                String task1Json = jyProblemReport.getTask1Json();
                String task2Name = jyProblemReport.getTask2Name();
                String task2Json = jyProblemReport.getTask2Json();
                String task3Name = jyProblemReport.getTask3Name();
                String task3Json = jyProblemReport.getTask3Json();
                String task4Name = jyProblemReport.getTask4Name();
                String task4Json = jyProblemReport.getTask4Json();
                String task5Name = jyProblemReport.getTask5Name();
                String task5Json = jyProblemReport.getTask5Json();
                String task6Name = jyProblemReport.getTask6Name();
                String task6Json = jyProblemReport.getTask6Json();
                String task7Name = jyProblemReport.getTask7Name();
                String task7Json = jyProblemReport.getTask7Json();
                Integer accountabilityCount = jyProblemReport.getAccountabilityCount();
                Integer regulationsPlanCount = jyProblemReport.getRegulationsPlanCount();
                Integer regulationsCompletedCount = jyProblemReport.getRegulationsCompletedCount();
                String regulationsPlanJson = jyProblemReport.getRegulationsPlanJson();
                String topThreeEvaluation = jyProblemReport.getTopThreeEvaluation();
                String bottomThreeEvaluation = jyProblemReport.getBottomThreeEvaluation();
                String remarks = jyProblemReport.getRemarks();
                params.put("prominentIssue"+id, prominentIssue==null?"":prominentIssue);
                params.put("issueType"+id, issueType==null?"":issueType);
                params.put("issueCategory"+id, issueCategory==null?"":issueCategory);
                params.put("leadingLeader"+id, leadingLeader==null?"":leadingLeader);
                params.put("leadingUnit"+id, leadingUnit==null?"":leadingUnit);
                params.put("task1Name"+id, task1Name==null?"":task1Name);
                params.put("task2Name"+id, task2Name==null?"":task2Name);
                params.put("task3Name"+id, task3Name==null?"":task3Name);
                params.put("task4Name"+id, task4Name==null?"":task4Name);
                params.put("task5Name"+id, task5Name==null?"":task5Name);
                params.put("task6Name"+id, task6Name==null?"":task6Name);
                params.put("task7Name"+id, task7Name==null?"":task7Name);
                params.put("accountabilityCount"+id, accountabilityCount==null?0:accountabilityCount);
                params.put("regulationsPlanCount"+id, regulationsPlanCount==null?0:regulationsPlanCount);
                params.put("regulationsPlanCount"+id, regulationsPlanCount==null?0:regulationsPlanCount);
                params.put("regulationsCompletedCount"+id, regulationsCompletedCount==null?0:regulationsCompletedCount);
                params.put("topThreeEvaluation"+id, topThreeEvaluation==null?"":topThreeEvaluation);
                params.put("bottomThreeEvaluation"+id, bottomThreeEvaluation==null?"":bottomThreeEvaluation);
                params.put("remarks"+id, remarks==null?"":remarks);
                if(StrUtil.isNotBlank(regulationsPlanJson)){
                    JSONArray regulationsPlanJsonList = JSONArray.parseArray(regulationsPlanJson);
                    StringBuilder regulationsPlanSB = new StringBuilder();
                    for (int i = 0; i < regulationsPlanJsonList.size(); i++) {
                        JSONObject regulationsPlanJsonObj = regulationsPlanJsonList.getJSONObject(i);
                        String name = regulationsPlanJsonObj.getString("name");
                        String status = regulationsPlanJsonObj.getString("status");
                        if ("1".equals(status)){
                            regulationsPlanSB.append("√[已建立] ");
                        }else if("0".equals(status)){
                            regulationsPlanSB.append("×[未建立] ");
                        }
                        regulationsPlanSB.append(name).append("\n");
                    }
                    String regulationsPlanString = regulationsPlanSB.toString();
                    params.put("regulationsPlanJson"+id, StrUtil.isNotBlank(regulationsPlanString)?regulationsPlanString:"");
                }else{
                    params.put("regulationsPlanJson"+id, "");
                }

                if(StrUtil.isNotBlank(task1Json)) {
                    if (isJsonArray(task1Json)) {
                        JSONArray task1JsonArray = JSONArray.parseArray(task1Json);
                        for (int i = 0; i < task1JsonArray.size(); i++) {
                            JSONObject task1JsonObject = (JSONObject) task1JsonArray.get(i);
                            params.put("task1Json" + id+"Name"+(i+1), task1JsonObject.getString("name"));
                            params.put("task1Json" + id+"Value"+(i+1), task1JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task1Json" + id, task1Json);
                    }
                }else{
                    params.put("task1Json"+id, "");
                }
                if(StrUtil.isNotBlank(task2Json)) {
                    if (isJsonArray(task2Json)) {
                        JSONArray task2JsonArray = JSONArray.parseArray(task2Json);
                        for (int i = 0; i < task2JsonArray.size(); i++) {
                            JSONObject task2JsonObject = (JSONObject) task2JsonArray.get(i);
                            params.put("task2Json" + id+"Name"+(i+1), task2JsonObject.getString("name"));
                            params.put("task2Json" + id+"Value"+(i+1), task2JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task2Json" + id, task2Json);
                    }
                }else{
                    params.put("task2Json"+id, "");
                }
                if(StrUtil.isNotBlank(task3Json)) {
                    if (isJsonArray(task3Json)) {
                        JSONArray task3JsonArray = JSONArray.parseArray(task3Json);
                        for (int i = 0; i < task3JsonArray.size(); i++) {
                            JSONObject task3JsonObject = (JSONObject) task3JsonArray.get(i);
                            params.put("task3Json" + id+"Name"+(i+1), task3JsonObject.getString("name"));
                            params.put("task3Json" + id+"Value"+(i+1), task3JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task3Json" + id, task3Json);
                    }
                }else{
                    params.put("task3Json"+id, "");
                }
                if(StrUtil.isNotBlank(task4Json)) {
                    if (isJsonArray(task4Json)) {
                        JSONArray task4JsonArray = JSONArray.parseArray(task4Json);
                        for (int i = 0; i < task4JsonArray.size(); i++) {
                            JSONObject task4JsonObject = (JSONObject) task4JsonArray.get(i);
                            params.put("task4Json" + id+"Name"+(i+1), task4JsonObject.getString("name"));
                            params.put("task4Json" + id+"Value"+(i+1), task4JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task4Json" + id, task4Json);
                    }
                }else{
                    params.put("task4Json"+id, "");
                }
                if(StrUtil.isNotBlank(task5Json)) {
                    if (isJsonArray(task5Json)) {
                        JSONArray task5JsonArray = JSONArray.parseArray(task5Json);
                        for (int i = 0; i < task5JsonArray.size(); i++) {
                            JSONObject task5JsonObject = (JSONObject) task5JsonArray.get(i);
                            params.put("task5Json" + id+"Name"+(i+1), task5JsonObject.getString("name"));
                            params.put("task5Json" + id+"Value"+(i+1), task5JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task5Json" + id, task5Json);
                    }
                }else{
                    params.put("task5Json"+id, "");
                }
                if(StrUtil.isNotBlank(task6Json)) {
                    if (isJsonArray(task6Json)) {
                        JSONArray task6JsonArray = JSONArray.parseArray(task6Json);
                        for (int i = 0; i < task6JsonArray.size(); i++) {
                            JSONObject task6JsonObject = (JSONObject) task6JsonArray.get(i);
                            params.put("task6Json" + id+"Name"+(i+1), task6JsonObject.getString("name"));
                            params.put("task6Json" + id+"Value"+(i+1), task6JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task6Json" + id, task6Json);
                    }
                }else{
                    params.put("task6Json"+id, "");
                }

                if(StrUtil.isNotBlank(task7Json)) {
                    if (isJsonArray(task7Json)) {
                        JSONArray task7JsonArray = JSONArray.parseArray(task7Json);
                        for (int i = 0; i < task7JsonArray.size(); i++) {
                            JSONObject task7JsonObject = (JSONObject) task7JsonArray.get(i);
                            params.put("task7Json" + id+"Name"+(i+1), task7JsonObject.getString("name"));
                            params.put("task7Json" + id+"Value"+(i+1), task7JsonObject.getString("value"));
                        }
                    } else {
                        params.put("task7Json" + id, task7Json);
                    }
                }else{
                    params.put("task7Json"+id, "");
                }
            }

            InputStream is = getClass().getClassLoader().getResourceAsStream("template/xxjyjzmb.xlsx");
            ExcelUtils excelUtils = new ExcelUtils();
            try {
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                response.setCharacterEncoding("utf-8");
                response.setHeader("Content-Disposition", "attachment;filename=exportData.xlsx");
                try (InputStream inputStream = excelUtils.replaceExcel(is, params);
                     ServletOutputStream out = response.getOutputStream()) {
                    IoUtil.copy(inputStream, out);
                    out.flush();
                } catch (IOException e) {
                    log.error("数据导出时发生 IO 异常", e);
                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                }
            } catch (Exception e) {
                log.error("数据导出时发生异常", e);
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Wrapper queryBackupData(JSONObject reqJson) {
        try{
            String backup = reqJson.getString("backup");
            DateTime backupDate = DateUtil.parse(backup);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("jzsj", backupDate);

            JyProblemReportBackup jyProblemReportBackup = new JyProblemReportBackup();
            String backupStr = DateUtil.format(backupDate, DatePattern.PURE_DATE_PATTERN);
            jyProblemReportBackup.setBackup(backupStr);
            List<JyProblemReportBackup> JyProblemReportBackups = jyProblemReportBackupMapper.select(jyProblemReportBackup);
            List<JyProblemReport> jyProblemReports = jyProblemReportMapper.selectAll();
            List<JyProblemReport> jyProblemReportNews = CollectionUtil.newArrayList();
            for (int i = 0; i < jyProblemReports.size(); i++) {
                JyProblemReport jyProblemReport = jyProblemReports.get(i);
                Long id = jyProblemReport.getId();
                String prominentIssue = jyProblemReport.getProminentIssue();
                List<JyProblemReportBackup> jyProblemReportBackups = JyProblemReportBackups.stream().filter(bean -> bean.getProminentIssue().equals(prominentIssue)).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(jyProblemReportBackups)){
                    JyProblemReportBackup jyProblemReportBackup1 = jyProblemReportBackups.get(0);
                    jyProblemReportBackup1.setId(null);
                    BeanUtil.copyProperties(jyProblemReportBackup1, jyProblemReport);
                    jyProblemReport.setId(id);
                    jyProblemReportNews.add(jyProblemReport);
                }
            }

            // 获取最新date
            Long bfTime = 0l;
            if (CollectionUtil.isEmpty(jyProblemReportNews)){
                return WrapMapper.ok();
            }
            for (JyProblemReport jyProblemReport : jyProblemReportNews) {
                Long time = jyProblemReport.getReportingTime().getTime();
                if (time>bfTime){
                    bfTime = time;
                }
            }
            return WrapMapper.ok(new PageInfo<>(jyProblemReportNews));
        } catch (Exception e) {
            e.printStackTrace();
            return WrapMapper.error(e.getMessage());
        }
    }

    public static boolean isJsonArray(String str) {
        if (str == null || str.trim().isEmpty()) {
            return false;
        }
        try {
            ObjectMapper mapper = new ObjectMapper();
            Object obj = mapper.readValue(str, Object.class);
            return obj instanceof java.util.List;
        } catch (Exception e) {
            return false;
        }
    }


}
